package com.dhecp.project.business.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.base.domain.BaseInfo;
import com.dhecp.project.base.service.BaseInfoService;
import com.dhecp.project.business.domain.LogisticsMmodeGrads;
import com.dhecp.project.business.domain.LogsticsPattern;
import com.dhecp.project.business.domain.LogsticsPatternDetails;
import com.dhecp.project.business.mapper.LogsticsPatternDetailsMapper;
import com.dhecp.project.business.mapper.LogsticsPatternMapper;
import com.dhecp.project.business.service.LogsticsPatternService;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.logstics.mapper.LogsticsMmodeGradsMapper;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 物流名称 服务层
 * @Author fxz
 * @Date 2022/9/22
 */
@Service
public class LogsticsPatternServicelmpl extends BaseModuleServiceImpl implements LogsticsPatternService {


    @Autowired
    private LogsticsPatternMapper logsticsPatternMapper;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    @Autowired
    private LogsticsPatternDetailsMapper logsticsPatternDetailsMapper;

    @Autowired
    private BaseInfoService baseInfoService;

    @Autowired
    private LogsticsMmodeGradsMapper logisticsMmodeGradsMapper;


    /**
     * 查询物流名称
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public List<LogsticsPattern> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句

        if (params.get("field") == null || params.get("field") == ""){
            String orderBy = "t1.createTime asc";
            PageUtils.startPageOrder(orderBy);
        }else{
            PageUtils.startPage();
        }

        return logsticsPatternMapper.findLogsticsPattern(params);
    }


    /**
     * 新增物流名称
     * @param params
     * @return
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public AjaxResult insertLogsticsServer(Map<String, Object> params) {
        if (StringUtils.isEmpty(params)) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }

        LogsticsPattern entity = JSON.parseObject(params.get("infoMap").toString(), LogsticsPattern.class);
        int count = logsticsPatternMapper.countByCode(entity);
        if (count != 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("物流编码重复,请核实再操作！");
        }
        if (entity.getCoefficientWeight().equals("0")){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("抛重系数不能为0,请核实再操作！");
        }
        if (entity.getPriceSingleBoxReq() == null){
            entity.setPriceSingleBoxReq(BigDecimal.ZERO);
        }
        entity.setGuid(RandomValueUtils.getGUID());
        entity.setCreateUser(ShiroUtils.getUserId());
        entity.setCreateTime(new Date());
        entity.setUpdateUser(ShiroUtils.getUserId());
        entity.setUpdateTime(new Date());
        if (entity.getGuid() == null || "".equals(entity.getGuid())){
            entity.setGuid(RandomValueUtils.getGUID());
        }

        if (entity.getCode() == null || entity.getCode().length() == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }

        if (entity.getStateShareTicket().equals("是")){
            entity.setStateShareTicket("1");
        } else if (entity.getStateShareTicket().equals("否")){
            entity.setStateShareTicket("0");
        } else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("可否拼票只允许填写是与否，请核实再操作！");
        }

        if (!entity.getLogisticsType().equals("物流") && !entity.getLogisticsType().equals("快递")){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("物流类型只允许填写快递与物流，请核实再操作！");
        }
        if (!entity.getNameLogsticsClearance().equals("包税") && !entity.getNameLogsticsClearance().equals("自税")){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("清关方式只允许填写包税与自税，请核实再操作！");
        }


        //新增物流名称明细
        List<LogsticsPatternDetails> onSonData = JSON.parseArray(params.get("infoItems").toString(), LogsticsPatternDetails.class);
        List<LogsticsPatternDetails> logsticsPatternDetailsList = new ArrayList<>();
        for (LogsticsPatternDetails logsticsPatternDetail : onSonData){
            logsticsPatternDetail.setGuid(RandomValueUtils.getGUID());
            logsticsPatternDetail.setCode(entity.getCode());
            logsticsPatternDetail.setCreateUser(ShiroUtils.getUserId());
            logsticsPatternDetail.setCreateTime(new Date());
            logsticsPatternDetail.setDeleted(false);
            logsticsPatternDetailsList.add(logsticsPatternDetail);
        }
        //判断梯度列表是否正确
        List<LogisticsMmodeGrads> grads = JSON.parseArray(params.get("grads").toString(), LogisticsMmodeGrads.class);
        List<LogisticsMmodeGrads> gradsList;
        List<LogisticsMmodeGrads> gradsList1;
        for (LogisticsMmodeGrads logisticsMmodeGrads : grads){
            if (logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads.getEndSection()) >= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("开始值不允许小于等于结束值");
            }
            gradsList = grads.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            gradsList1 = grads.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            if (gradsList.size() != 0 || gradsList1.size() != 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("区间有重叠");
            }
        }

        List<LogisticsMmodeGrads> logisticsMmodeGradsList = new ArrayList<>();
        for (LogisticsMmodeGrads logisticsMmodeGrads : grads){
            logisticsMmodeGrads.setGuid(RandomValueUtils.getGUID());
            logisticsMmodeGrads.setLogisticsMmodeGuid(entity.getGuid());
            logisticsMmodeGrads.setType("WLMC");
//            logisticsMmodeGradsMapper.insertLogisticsMmodeGrads(logisticsMmodeGrads);
            logisticsMmodeGradsList.add(logisticsMmodeGrads);
        }

        //判断计重明细列表是否正确
        List<LogisticsMmodeGrads> infoItemsZj = JSON.parseArray(params.get("infoItemsZj").toString(), LogisticsMmodeGrads.class);
        List<LogisticsMmodeGrads> infoItemsList;
        List<LogisticsMmodeGrads> infoItemsList1;
        for (LogisticsMmodeGrads logisticsMmodeGrads : infoItemsZj){
            if (logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads.getEndSection()) >= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("开始值不允许小于等于结束值");
            }
            infoItemsList = infoItemsZj.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            infoItemsList1 = infoItemsZj.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            if (infoItemsList.size() != 0 || infoItemsList1.size() != 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("区间有重叠");
            }
        }

        List<LogisticsMmodeGrads> logisticsMmodeInfoItemsList = new ArrayList<>();
        for (LogisticsMmodeGrads logisticsMmodeGrads : infoItemsZj){
            logisticsMmodeGrads.setGuid(RandomValueUtils.getGUID());
            logisticsMmodeGrads.setLogisticsMmodeGuid(entity.getGuid());
            logisticsMmodeGrads.setType("WLMCTDJZ");
            logisticsMmodeInfoItemsList.add(logisticsMmodeGrads);
        }

        logsticsPatternMapper.insertLogsticsPattern(entity);

        logsticsPatternDetailsMapper.insertLogsticsPatternDetailsBatch(logsticsPatternDetailsList);

        logisticsMmodeGradsMapper.insertLogisticsMmodeGradsBatch(logisticsMmodeGradsList);

        logisticsMmodeGradsMapper.insertLogisticsMmodeGradsBatch(logisticsMmodeInfoItemsList);
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"),entity.getGuid());
    }


    /**
     * @Description: 查询指定单nid的首记录
     * @Param: [params]
     * @Author: fxz
     * @date: 2022-09-22 19:00
     */
    @Override
    public LogsticsPattern queryFirstByGuid(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        return logsticsPatternMapper.queryFirstByGuid(params);
    }


    /**
     * 修改物流名称
     * @author fxz
     * @date: 2022-09-22
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public AjaxResult updateLogsticsPattern(Map<String, Object> params) {
        if (StringUtils.isEmpty(params)) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }

        LogsticsPattern entity = JSON.parseObject(params.get("infoMap").toString(), LogsticsPattern.class);
        int count = logsticsPatternMapper.countByCode(entity);
        if (count != 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("物流编码重复,请核实再操作！");
        }
        if (entity.getPriceSingleBoxReq() == null){
            entity.setPriceSingleBoxReq(BigDecimal.ZERO);
        }
        if (entity.getStateShareTicket().equals("是")){
            entity.setStateShareTicket("1");
        } else if (entity.getStateShareTicket().equals("否")){
            entity.setStateShareTicket("0");
        } else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("可否拼票只允许填写是与否，请核实再操作！");
        }

        if (!entity.getLogisticsType().equals("物流") && !entity.getLogisticsType().equals("快递")){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("物流类型只允许填写快递与物流，请核实再操作！");
        }
        if (!entity.getNameLogsticsClearance().equals("包税") && !entity.getNameLogsticsClearance().equals("自税")){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("清关方式只允许填写包税与自税，请核实再操作！");
        }
        entity.setUpdateUser(ShiroUtils.getUserId());
        entity.setUpdateTime(new Date());

        if (entity.getCode() == null || entity.getCode().length() == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }

        //判断梯度列表是否正确
        List<LogisticsMmodeGrads> grads = JSON.parseArray(params.get("grads").toString(), LogisticsMmodeGrads.class);
        List<LogisticsMmodeGrads> gradsList;
        List<LogisticsMmodeGrads> gradsList1;
        for (LogisticsMmodeGrads logisticsMmodeGrads : grads){
            if (logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads.getEndSection()) >= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("开始值不允许小于等于结束值");
            }
            gradsList = grads.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            gradsList1 = grads.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            if (gradsList.size() != 0 || gradsList1.size() != 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("区间有重叠");
            }
        }

        //判断计重明细列表是否正确
        List<LogisticsMmodeGrads> infoItemsZj = JSON.parseArray(params.get("infoItemsZj").toString(), LogisticsMmodeGrads.class);
        List<LogisticsMmodeGrads> infoItemsList;
        List<LogisticsMmodeGrads> infoItemsList1;
        for (LogisticsMmodeGrads logisticsMmodeGrads : infoItemsZj){
            if (logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads.getEndSection()) >= 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("开始值不允许小于等于结束值");
            }
            infoItemsList = infoItemsZj.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            infoItemsList1 = infoItemsZj.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
                    && logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
                    && !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
            ).collect(Collectors.toList());
            if (infoItemsList.size() != 0 || infoItemsList1.size() != 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("区间有重叠");
            }
        }

        List<LogisticsMmodeGrads> logisticsMmodeInfoItemsList = new ArrayList<>();
        for (LogisticsMmodeGrads logisticsMmodeGrads : infoItemsZj){
            logisticsMmodeGrads.setGuid(RandomValueUtils.getGUID());
            logisticsMmodeGrads.setLogisticsMmodeGuid(entity.getGuid());
            logisticsMmodeGrads.setType("WLMCTDJZ");
            logisticsMmodeInfoItemsList.add(logisticsMmodeGrads);
        }

        //删除物流方式梯度和计重明细列表
        LogisticsMmodeGrads logisticsMmodeGrads2 = new LogisticsMmodeGrads();
        logisticsMmodeGrads2.setLogisticsMmodeGuid(entity.getGuid());


        List<LogisticsMmodeGrads> logisticsMmodeGradsList = new ArrayList<>();
        for (LogisticsMmodeGrads logisticsMmodeGrads : grads){
            logisticsMmodeGrads.setGuid(RandomValueUtils.getGUID());
            logisticsMmodeGrads.setLogisticsMmodeGuid(entity.getGuid());
            logisticsMmodeGrads.setType("WLMC");
//            logisticsMmodeGradsMapper.insertLogisticsMmodeGrads(logisticsMmodeGrads);
            logisticsMmodeGradsList.add(logisticsMmodeGrads);
        }

        //删除物流名称明细
        LogsticsPatternDetails logsticsPatternDetails = new LogsticsPatternDetails();
        logsticsPatternDetails.setCode(entity.getCode());

        //新增物流名称明细
        List<LogsticsPatternDetails> onSonData = JSON.parseArray(params.get("infoItems").toString(), LogsticsPatternDetails.class);
        List<LogsticsPatternDetails> logsticsPatternDetailsList = new ArrayList<>();
        for (LogsticsPatternDetails logsticsPatternDetail : onSonData){
            logsticsPatternDetail.setGuid(RandomValueUtils.getGUID());
            logsticsPatternDetail.setCode(entity.getCode());
            logsticsPatternDetail.setCreateUser(ShiroUtils.getUserId());
            logsticsPatternDetail.setCreateTime(new Date());
            logsticsPatternDetail.setDeleted(false);
            logsticsPatternDetailsList.add(logsticsPatternDetail);
        }

        logsticsPatternMapper.updateByGuid(entity);

        logisticsMmodeGradsMapper.deleteByLogisticsMmodeGuid(logisticsMmodeGrads2);

        logisticsMmodeGradsMapper.insertLogisticsMmodeGradsBatch(logisticsMmodeGradsList);

        logsticsPatternDetailsMapper.deleteBycode(logsticsPatternDetails);

        int updateLength = logsticsPatternDetailsList.size();
        int i = 0;
        while (updateLength > 100) {
            logsticsPatternDetailsMapper.insertLogsticsPatternDetailsBatch(logsticsPatternDetailsList.subList(i, i + 100));

            i = i + 100;
            updateLength = updateLength - 100;
        }
        if (updateLength > 0) {
            logsticsPatternDetailsMapper.insertLogsticsPatternDetailsBatch(logsticsPatternDetailsList.subList(i, i + updateLength));
        }

        logisticsMmodeGradsMapper.insertLogisticsMmodeGradsBatch(logisticsMmodeInfoItemsList);

        return AjaxResult.success(MessageUtils.message("module.success.issuccess"),entity.getGuid());

    }

    /**
     * 删除物流名称
     * @author fxz
     * @date: 2022-09-22
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public AjaxResult deleteLogsticspattern(Map<String, Object> params) {
        List<LogsticsPattern> logsticsPatternList = JSON.parseArray(params.get("infoItems").toString(), LogsticsPattern.class);
        if (logsticsPatternList == null || logsticsPatternList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
        }
        for (LogsticsPattern logsticsPattern : logsticsPatternList) {
            logsticsPatternMapper.deleteByGuid(logsticsPattern);

            //删除物流名称明细
            LogsticsPatternDetails logsticsPatternDetails = new LogsticsPatternDetails();
            logsticsPatternDetails.setCode(logsticsPattern.getCode());
            logsticsPatternDetailsMapper.deleteBycode(logsticsPatternDetails);

            //删除物流方式梯度
            LogisticsMmodeGrads logisticsMmodeGrads2 = new LogisticsMmodeGrads();
            logisticsMmodeGrads2.setLogisticsMmodeGuid(logsticsPattern.getGuid());
            logisticsMmodeGradsMapper.deleteByLogisticsMmodeGuid(logisticsMmodeGrads2);
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    /**
     * 同步材质
     * @author fxz
     * @date: 2023-02-27
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public AjaxResult hotMaterial(Map<String, Object> params) throws Exception {
        List<LogsticsPattern> logsticsPatternList = JSON.parseArray(params.get("infoItems").toString(), LogsticsPattern.class);
        if (logsticsPatternList == null || logsticsPatternList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
        }
        Map<String, Object> param1 = new HashMap<String, Object>();
        Map<String, Object> param2 = new HashMap<String, Object>();
        List<LogsticsPatternDetails> logsticsPatternDetails1;
        List<LogsticsPatternDetails> logsticsPatternDetailsNew;
        for (LogsticsPattern logsticsPattern : logsticsPatternList) {
            param1.put("code",logsticsPattern.getCode());
            //查询当前的物流详情
            logsticsPatternDetails1 = logsticsPatternDetailsMapper.findLogsticsPatternDetails(param1);

            param2.put("vtype", "spcz");
            param2.put("moduleTable", "TB_baseinfo_spcz");
            //查询当前材质
            List<BaseInfo> list  = baseInfoService.queryBaseInfoList(param2);
            List<LogsticsPatternDetails> logsticsPatternDetailsList = new ArrayList<>();
            LogsticsPatternDetails logsticsPatternDetails;
            for (BaseInfo baseInfo : list){
                logsticsPatternDetailsNew =
                        logsticsPatternDetails1.stream().filter(logsticsPatternDetail -> logsticsPatternDetail.getMaterialName().equals(baseInfo.getVname()))
                                .collect(Collectors.toList());


                if (baseInfo.getBdeletedtext().equals("否")) {
                    logsticsPatternDetails = new LogsticsPatternDetails();
                    if (logsticsPatternDetailsNew.size() == 0) {
                        //新增
                        logsticsPatternDetails.setGuid(RandomValueUtils.getGUID());
                        logsticsPatternDetails.setCode(logsticsPattern.getCode());
                        logsticsPatternDetails.setMaterialName(baseInfo.getVname());
                        logsticsPatternDetails.setMaterialCost(BigDecimal.ZERO);
                        logsticsPatternDetails.setDeleted(false);
                        logsticsPatternDetails.setCreateTime(new Date());
                        logsticsPatternDetails.setCreateUser(ShiroUtils.getUserId());
                        logsticsPatternDetailsList.add(logsticsPatternDetails);
                    }
                }
            }
            //需要删除的
            List<String> baseInfoDel = new ArrayList<>();
            List<BaseInfo> baseInfoList = new ArrayList<>();
            for (LogsticsPatternDetails logsticsPatternDetails2 : logsticsPatternDetails1){
                baseInfoList =
                        list.stream().filter(baseInfo1 -> logsticsPatternDetails2.getMaterialName().equals(baseInfo1.getVname()))
                                .collect(Collectors.toList());
                if (baseInfoList.size() == 0){
                    baseInfoDel.add(logsticsPatternDetails2.getMaterialName());
                }
            }
            //根据code批量删除
            Map<String, Object> deleteMap = new HashMap<>();
            int deleteLength = baseInfoDel.size();
            int j = 0;
            while (deleteLength > 1000) {
                deleteMap.put("code",logsticsPattern.getCode());
                deleteMap.put("materialName", baseInfoDel.subList(j, j + 1000));
                logsticsPatternDetailsMapper.deleteLogsticsPatternDetailsMap(deleteMap);
                j = j + 1000;
                deleteLength = deleteLength - 1000;
            }
            if (deleteLength > 0) {
                deleteMap.put("code",logsticsPattern.getCode());
                deleteMap.put("materialName", baseInfoDel.subList(j, j + deleteLength));
                logsticsPatternDetailsMapper.deleteLogsticsPatternDetailsMap(deleteMap);
            }
            //批量新增
            int insertLength = logsticsPatternDetailsList.size();
            int i = 0;
            while (insertLength > 90) {
                logsticsPatternDetailsMapper.insertLogsticsPatternDetailsBatch(logsticsPatternDetailsList.subList(i, i + 90));
                i = i + 90;
                insertLength = insertLength - 90;
            }
            if (insertLength > 0) {
                logsticsPatternDetailsMapper.insertLogsticsPatternDetailsBatch(logsticsPatternDetailsList.subList(i, i + insertLength));
            }
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }
}
